//
//  CheckLoginObject.m
//  ZPSALE
//
//  Created by fei on 14-9-5.
//  Copyright (c) 2014年 self. All rights reserved.
//

#import "CheckLoginObject.h"
static CheckLoginObject *_checkLogin;
@implementation CheckLoginObject
+(CheckLoginObject *)shareCheckLogin {
    if (!_checkLogin) {
        _checkLogin = [[CheckLoginObject alloc] init];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(responseNotification:) name:StringForCheckLogin object:nil];
    }
    
    return _checkLogin;
}
+(void)responseNotification:(NSNotification *)noti {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/UserInfo.plist"];
    if (![fileManager fileExistsAtPath:homePath]) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"UserInfo" ofType:@"plist"];
        [fileManager copyItemAtPath:path toPath:homePath error:nil];
    }
    NSMutableDictionary *dataDic = [[NSDictionary dictionaryWithContentsOfFile:homePath] mutableCopy];
    NSString *userName = dataDic[@"userName"];
    NSString *password = dataDic[@"passwd"];
    
    if ([userName isEqual:[NSNull null]] || [userName length] <=0 || [password isEqual:[NSNull null]] || [password length] <=0) {
        [CheckLoginObject saveLoginWithStatus:NO];
        return;
    }else{
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *params = @{@"username": userName,
                                 @"password": password,
                                 };
        
        [manager POST:APIURL@"oauth/getAccessToken.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *tokeS = responseObject[@"access_token"];
            if ([tokeS isEqual:[NSNull null]] || tokeS.length == 0) {
                
                [CheckLoginObject saveLoginWithStatus:NO];
            } else {
                
                [dataDic setValue:tokeS forKey:@"token"];
                [dataDic writeToFile:homePath atomically:YES];
                [CheckLoginObject saveLoginWithStatus:YES];
            }
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [CheckLoginObject saveLoginWithStatus:NO];//接口握手失败标记
        }];
    }
}

+(void)saveLoginWithStatus:(BOOL)isLogin {
    NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
    [userD setValue:[NSNumber numberWithBool:isLogin] forKey:LoginStatus];
}
+(BOOL)isLogin {
    NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
    BOOL isLogin = [[userD valueForKey:LoginStatus] boolValue];
    NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/UserInfo.plist"];
    NSMutableDictionary *dataDic = [[NSDictionary dictionaryWithContentsOfFile:homePath] mutableCopy];
    NSString *memberid = [dataDic valueForKey:@"memberId"];
    NSString *token = [dataDic valueForKey:@"token"];
    if (([memberid isEqual:[NSNull null]] || memberid.length == 0 || [token isEqual:[NSNull null]] || token.length == 0) || !isLogin) {
        isLogin = NO;
    } else {
        isLogin = YES;
    }
    return isLogin;
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:StringForCheckLogin object:nil];
}
@end
